<?php
/**
 * Created by PhpStorm.
 * User: Thinkpad
 * Date: 2015/6/29
 * Time: 10:08
 */

namespace Admin\Controller;
use Think\Controller;

class OrderController extends Controller{

    private $OrderInfo='';
    private $OrderGoods='';
    private $Seller='';
    private $goods='';
    function __construct()
    {
        parent::__construct();
        $this->OrderInfo=D("Order_info");
        $this->OrderGoods=D("Order_goods");
        $this->Seller=D("seller");
        $this->goods=D("Goods");
        $this->OrderInfo->startTrans();
    }

    function index($condition=''){
        if($_POST)
        {
            if($condition==1) {
                $count = $this->OrderInfo->where("pay_status=" . $_POST['pay_status'])->count();
                $alreadyCount = $this->OrderInfo->where("pay_status=2")->count();
                $Page = new \Think\Page($count, 20);
                $nowPage = isset($_GET['p']) ? $_GET['p'] : 1;
                $arr = $this->OrderInfo->where("pay_status=" . $_POST['pay_status'])->order(array('id', 'id' => 'desc'))->select();
                $sumAmount = $this->OrderInfo->where("pay_status=" . $_POST['pay_status'])->sum('goods_amount');
                $alreadySumAmount = $this->OrderInfo->where("pay_status=2")->sum("goods_amount");
                if($count&&$arr&&$sumAmount)
                {
                    $this->OrderInfo->commit();
                }else{
                    $this->OrderInfo->rollback();
                }
                $this->assign("pay_status",$_POST['pay_status']);
                $this->assign("countOrderNum",$count);
                $this->assign("sumAmount",$sumAmount);
                $this->assign("alreadySumAmount",$alreadySumAmount);
                $this->assign("alreadyCount",$alreadyCount);
            }
            elseif($condition==2)
            {
                $count=$this->OrderInfo->where("pay_status=".$_POST['pay_type'])->count();
                $alreadyCount = $this->OrderInfo->where("pay_status=2")->count();
                $Page=new \Think\Page($count,20);
                $nowPage=isset($_GET['p'])?$_GET['p']:1;
                $arr=$this->OrderInfo->where("pay_status=".$_POST['pay_type'])->order(array('id','id'=>'desc'))->select();
                $sumAmount=$this->OrderInfo->where("pay_status=".$_POST['pay_type'])->sum('goods_amount');
                $alreadySumAmount = $this->OrderInfo->where("pay_status=2")->sum("goods_amount");
                if($count&&$arr&&$sumAmount)
                {
                    $this->OrderInfo->commit();
                }else{
                    $this->OrderInfo->rollback();
                }
                $this->assign("pay_type",$_POST['pay_type']);
                $this->assign("sumAmount",$sumAmount);
                $this->assign("countOrderNum",$count);
                $this->assign("alreadySumAmount",$alreadySumAmount);
                $this->assign("alreadyCount",$alreadyCount);
            }
            elseif($condition==3)   // 通过时间判断
            {
                $promote_start_date=$_POST['startTime'];
                $yanG1=explode("-",$promote_start_date);
                $yanGstart=mktime(0,0,0,$yanG1[0],$yanG1[1],$yanG1[2]);      // 得到促销开始的时间戳
                $promote_end_date=$_POST['endTime'];
                $yanG2=explode("-",$promote_end_date);
                $yanGend=mktime(0,0,0,$yanG2[0],$yanG2[1],$yanG2[2]);      // 得到促销结束的时间戳
                if($yanGstart>=$yanGend)
                {
                    $this->error("时间范围设置不正确","Order/index");
                }else{
                    $cond['add_time']=array("GT",$yanGstart);
                    $cond['add_time']=array("LT",$yanGend);
                    $count=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend}")->count();
                    $alreadyCount = $this->OrderInfo->where("pay_status=2")->count();
                    $Page=new \Think\Page($count,20);
                    $nowPage=isset($_GET['p'])?$_GET['p']:1;
                    $arr=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend}")->order(array('id','id'=>'desc'))->select();
                    $sumAmount=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend}")->sum('goods_amount');
                    $alreadySumAmount = $this->OrderInfo->where("pay_status=2")->sum("goods_amount");
                    if($count&&$arr&&$sumAmount)
                    {
                        $this->OrderInfo->commit();
                    }else{
                        $this->OrderInfo->rollback();
                    }
                    $this->assign("sumAmount",$sumAmount);
                    $this->assign("countOrderNum",$count);
                    $this->assign("alreadySumAmount",$alreadySumAmount);
                    $this->assign("alreadyCount",$alreadyCount);
                }
            }
            elseif($condition==4)
            {
                $count=$this->OrderInfo->where("order_sn=".$_POST['order_sn'])->count();
                $alreadyCount = $this->OrderInfo->where("order_sn=".$_POST['order_sn'] AND "pay_status=2")->count();
                $Page=new \Think\Page($count,20);
                $nowPage=isset($_GET['p'])?$_GET['p']:1;
                $arr=$this->OrderInfo->where("order_sn=".$_POST['order_sn'])->select();
                $sumAmount=$this->OrderInfo->where("order_sn=".$_POST['order_sn'])->sum('goods_amount');
                $alreadySumAmount = $this->OrderInfo->where("order_sn=".$_POST['order_sn'] AND "pay_status=2")->sum("goods_amount");
                if($count&&$arr&&$sumAmount)
                {
                    $this->OrderInfo->commit();
                }else{
                    $this->OrderInfo->rollback();
                }
                $this->assign("sumAmount",$sumAmount);
                $this->assign("countOrderNum",$count);
                $this->assign("alreadySumAmount",$alreadySumAmount);
                $this->assign("alreadyCount",$alreadyCount);
            }
            elseif($condition==5)
            {
                $promote_start_date=$_POST['startTime_b'];
                $yanG1=explode("-",$promote_start_date);
                $yanGstart=mktime(0,0,0,$yanG1[0],$yanG1[1],$yanG1[2]);      // 得到促销开始的时间戳

                $promote_end_date=$_POST['endTime_b'];
                $yanG2=explode("-",$promote_end_date);
                $yanGend=mktime(0,0,0,$yanG2[0],$yanG2[1],$yanG2[2]);      // 得到促销结束的时间戳
                if($yanGstart>$yanGend)
                {
                    $this->error("时间范围设置不正确","Order/index");
                }else{
                    $cond['add_time']=array("GT",$yanGstart);
                    $cond['add_time']=array("LT",$yanGend);
                    $count=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend} AND seller_id={$_POST['sellerID']}")->count();    // order_status=2 表示 交易成功
                    $alreadyCount=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend} AND seller_id={$_POST['sellerID']} AND order_status=2 AND pay_status=2")->count();
                    $Page=new \Think\Page($count,20);
                    $nowPage=isset($_GET['p'])?$_GET['p']:1;
                    $arr=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend} AND seller_id={$_POST['sellerID']}")->order(array('id','id'=>'asc'))->select();
                    $sumAmount=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend} AND seller_id={$_POST['sellerID']} AND order_status=2")->sum('goods_amount');
                    $alreadySumAmount=$this->OrderInfo->where("add_time>{$yanGstart} AND add_time<{$yanGend} AND seller_id={$_POST['sellerID']} AND order_status=2 AND pay_status=2")->sum('goods_amount');
                    if($count&&$arr&&$sumAmount)
                    {
                        $this->OrderInfo->commit();
                    }else{
                        $this->OrderInfo->rollback();
                    }
                    $this->assign("countOrderNum",$count);
                    $this->assign("sumAmount",$sumAmount);
                    $this->assign("alreadySumAmount",$alreadySumAmount);
                    $this->assign("alreadyCount",$alreadyCount);
                }
            }
            elseif($condition==6)
            {
                $count = $this->OrderInfo->where("order_status=".$_POST['order_status'])->count();
                $alreadyCount=$this->OrderInfo->where("order_status=".$_POST['order_status'] AND "pay_status=2")->count();
                $Page = new \Think\Page($count, 20);
                $nowPage = isset($_GET['p']) ? $_GET['p'] : 1;
                $arr = $this->OrderInfo->where("order_status=" . $_POST['order_status'])->order(array('id', 'id' => 'desc'))->select();
                $sumAmount = $this->OrderInfo->where("order_status=" . $_POST['order_status'])->sum('goods_amount');
                $alreadySumAmount=$this->OrderInfo->where("order_status=".$_POST['order_status'] AND "pay_status=2")->sum('goods_amount');
                if($count&&$arr&&$sumAmount)
                {
                    $this->OrderInfo->commit();
                }else{
                    $this->OrderInfo->rollback();
                }
                $this->assign("order_status",$_POST['order_status']);
                $this->assign("countOrderNum",$count);
                $this->assign("sumAmount",$sumAmount);
                $this->assign("alreadySumAmount",$alreadySumAmount);
                $this->assign("alreadyCount",$alreadyCount);
            }
        }else{
            $count=$this->OrderInfo->count();
//            $Page=new \Think\Page($count,20);
//            $nowPage=isset($_GET['p'])?$_GET['p']:1;
            $arr=$this->OrderInfo->order(array('id','id'=>'desc'))->select();
            $sumAmount=$this->OrderInfo->sum('goods_amount');
            $this->OrderInfo->commit();
            $this->assign("sumAmount",$sumAmount);
            $this->assign("countOrderNum",$count);
        }
        foreach($arr as $key=>$r)
        {
            $yanG1=D('Users')->field("nick")->where("id=".$r['user_id'])->find();
            $arr[$key]['username']=$yanG1['nick'];
            $arr[$key]['order_status_b']=$r['order_status']==2?"交易成功":"交易未成功";
            $arr[$key]['pay_status_b']=$r['pay_status']==2?"支付成功":"未支付";
            if($r['pay_type']=="0")
            {
                $arr[$key]['pay_type_b']="货到付款";
            }elseif($r['pay_type']=="1")
            {
                $arr[$key]['pay_type_b']="微信支付";
            }
            $arr[$key]['return_money_b']=$r['return_money']==1?"<font color='#FF0000'>已回款</font>":"无";
            $arr[$key]['goods_amount_b']=$r['goods_amount'];
            $arr[$key]['add_time_b']=date("Y-m-d H:i:s",$r['add_time']);
        }
        //$show=$Page->show();
        $seller=$this->Seller->field(array('id','nick'))->select();
        $this->assign("seller",$seller);
//        $this->assign('page',$show);
        $this->assign("arr",$arr);
        $this->display();
    }



    // 得到 订单的详细信息
    function orderInfo($id='')
    {
        $cond['id']=$id;
        $yanG1=$this->OrderInfo->where($cond)->find();  // 得到相应的订单信息
        $sellerInfo=$this->Seller->where("id=".$yanG1['seller_id'])->find();
        $yanG1['order_status_b']=$yanG1['order_status']==2?"交易成功":"交易未成功";
        $yanG1['pay_status_b'] = $yanG1['pay_status'] == 2 ? "支付成功" : "未支付";
        if($yanG1['pay_type']=="0")
        {
            $yanG1['pay_type_b']="货到付款";
        }elseif($yanG1['pay_type']=="1")
        {
            $yanG1['pay_type_b']="微信支付";
        }
        if($yanG1['shipping_type']=="1")   // 门店自提
        {
            $yanG1['shipping_type_b']="门店自提(".$sellerInfo['nick']."门店地址".$sellerInfo['address'].")";
        }else{
            $yanG1['shipping_type_b']="快递";
        }
        $yanG1['return_money_b'] = $yanG1['return_money']==1?"<font color='#FF0000' weight='900'>已回款</font>":"<font color='#666666'>未回款</font>";

        $yanG1['add_time_b']=date("Y-m-d  H:i:s",$yanG1['add_time']);
        $yanG1['sellerName']=$sellerInfo['nick'];
        $this->assign("data",$yanG1);

        //订单中的商品
        $yanG2=$this->OrderGoods->where("order_id=".$id)->select();
        foreach($yanG2 as $key=>$r)
        {
            $yanG3=$this->goods->field("id","name","thumb")->where("id=".$r['goods_id'])->find();
            $yanG2[$key]['goodsName']=$yanG3['name'];
            $yanG2[$key]['thumb']=$yanG3['thumb'];
        }

        $this->assign("yanG2",$yanG2);
        //END

        $this->display("orderInfo");
    }

    // 此订单已经支付给商户 的相关操作
    function returnMoney()
    {
        foreach($_POST['returnMoney'] as $r)
        {
            $data['return_money']=1;
            $this->OrderInfo->where("id=".$r)->save($data);
            $this->OrderInfo->commit();
        }
        $this->redirect("Order/index");
        echo "this->index"; exit;
    }
}